package Gui.Dialogs;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Cards.Card;
import Cards.MailCardBill;
import Gui.CheckPanel;
import Gui.GameFrame;
import Main.Defines;

public class PayDayDialog extends JDialog
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 5576051974883456418L;

	private JPanel Top;
	private JPanel Center;
	private JPanel Bottom;

	// Mail Stuff
	private CheckPanel cp = null;
	int numBills = 0;
	private LinkedList<MailCardBill> BillCards;
	int PayoffAmount = 0;
	int InterestAmount = 0;

	public PayDayDialog()
	{
		setModalityType(getModalityType().APPLICATION_MODAL);
		Update();
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
	}

	public void Update()
	{		
		setVisible(false);
		setLayout(new BorderLayout());
		Top = new JPanel();
		Center = new JPanel();
		Bottom = new JPanel();
		BillCards = new LinkedList<MailCardBill>();
		BuildBillList();
		DisplayLoans();
		pack();
		setLocationRelativeTo(GameFrame.getInstance());
		setVisible(true);
	}
	
	public void BuildBillList()
	{		
		String[] tempBills = new String[Defines.CP().getMailCards().size()];
		numBills = 0;
		for (int i = 0; i < Defines.CP().getMailCards().size(); i++)
		{
			if (Defines.CP().getMailCards().get(i).getM_cardType() == Card.MailCardBill)
			{
				tempBills[numBills++] = Defines.CP().getMailCards().get(i).toString();
				BillCards.add((MailCardBill)Defines.CP().getMailCards().get(i));
			}
		}
		String[] Bills = new String[numBills];
		for (int x=0; x<numBills; x++)
		{
			Bills[x] = tempBills[x];
			PayoffAmount += BillCards.get(x).getValue();
		}
		
		//If the user doesn't have any bills don't display the bill dialog
		if (numBills == 0)
		{
			JLabel L2 = new JLabel("You have 0 Bills");
			add(L2);
		} else
		{
			//Display the Bills and Payoff Button
			Top.setLayout(new GridLayout(0,1));	
			JLabel MailTitle = new JLabel("You have " + numBills + " Bill(s)");
			JLabel MailTitle2 = new JLabel("Check all the ones you want to pay");
			JLabel MailTitle3 = new JLabel("(10% Interest Charge for not paying bills)");						
			Top.add(MailTitle);
			Top.add(MailTitle2);
			Top.add(MailTitle3);			
			add(Top, BorderLayout.NORTH);
			
			//Center.setLayout(new GridLayout(0,1));
			cp = new CheckPanel(Bills);
			Center.add(cp);
			
			JButton Payoff = new JButton("Payoff Bills");
			Payoff.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{					
					if ((cp != null) && (Defines.CP().getCash() >= PayoffAmount)) {
						for (int x=0; x<numBills; x++)
						{
							if (cp.isSelected(x))
							{
								Defines.CP().getMailCards().remove(BillCards.get(x));
							}
						}
						Defines.CP().RemoveCash(PayoffAmount);
					}
					dispose();
					@SuppressWarnings("unused")
					PayDayDialog showDialog1 = new PayDayDialog();
				}
			});
			Center.add(Payoff);
			add(Center, BorderLayout.CENTER);
		}
	}

	public void DisplayLoans()
	{
		Bottom.setLayout(new GridLayout(0,1));
		if (Defines.CP().getLoan() > 0)
		{
			JLabel LoansTitle = new JLabel("Your loan balance is Currently: " + Defines.CP().getLoan());
			Bottom.add(LoansTitle);
			
			JButton Payoff = new JButton("Payoff Loans");
			Payoff.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{					
					@SuppressWarnings("unused")
					ViewLoansDialog showDialog = new ViewLoansDialog();
				}
			});
			Bottom.add(Payoff);
		}
			JButton Done = new JButton("Done");
			Done.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					if (numBills > 0)
					{
						InterestAmount = 0;
						//Calculate the cost of interest charge
						for (int x=0; x<numBills; x++)
						{
							
							InterestAmount += (BillCards.get(x).getCost() *.1);
						}
						//Check if this is the players final month.  if so see if its the end of the game for everyone
						if (Defines.CP().getMonth()==Defines.GM.getNumberOfMonths())
						{
							Defines.CP().RemoveCash(InterestAmount);
							Defines.CP().setDone(true);
							int counter = 0;
							//check if everyone is done, if so, signal game over
							for (int x=0; x<Defines.GM.getNumberOfPlayers();x++)
							{
								if (Defines.PLM.getPlayerList().get(x).getDone()==true)
								{
									counter++;
								}
							}
							System.out.println(counter + "\n");
							System.out.println(Defines.GM.getNumberOfPlayers()+"\n");
							if (counter==Defines.GM.getNumberOfPlayers())
							{
								Defines.EVM.queueEvent("EVENT_GAME_OVER");
							}
							dispose();
						}
						else
						{
							if (Defines.CP().getCash() >= InterestAmount)
							{
								Defines.CP().RemoveCash(InterestAmount);
							}
							else
							{
								System.out.println("Prompt Player");
							}
							Defines.CP().setDay(0);
							Defines.CP().setMonth(Defines.CP().getMonth()+1);
							dispose();
						}
					}
					else
					{
						if (Defines.CP().getMonth()==Defines.GM.getNumberOfMonths())
						{
							Defines.CP().setDone(true);
							int counter = 0;
							//check if everyone is done, if so, signal game over
							for (int x=0; x<Defines.GM.getNumberOfPlayers();x++)
							{
								if (Defines.PLM.getPlayerList().get(x).getDone()==true)
								{
									counter++;
								}
							}
							System.out.println(counter + "\n");
							System.out.println(Defines.GM.getNumberOfPlayers()+"\n");
							if (counter==Defines.GM.getNumberOfPlayers())
							{
								Defines.EVM.queueEvent("EVENT_GAME_OVER");
							}							
						}
						else
						{
							Defines.CP().setDay(0);
							Defines.CP().setMonth(Defines.CP().getMonth()+1);
						}
						dispose();
					}
				}
			});	
			
			Bottom.add(Done);					
			add(Bottom, BorderLayout.SOUTH);
	}
}